package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormSteps;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormStepsMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtWFlowFormStepsMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormStepsService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程表单-流程步骤 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-19
 */
@Service("wFlowFormStepsService")
public class WFlowFormStepsServiceImpl extends ServiceImpl<WFlowFormStepsMapper, WFlowFormSteps> implements WFlowFormStepsService {

    @Autowired
    private ExtWFlowFormStepsMapper extWFlowFormStepsMapper;

    /***
     * 根据条件获取WFlowFormSteps列表
     */
	@Override
	public List<WFlowFormSteps> getWFlowFormStepsByCnd(Map<String, Object> mapParams) {
		return extWFlowFormStepsMapper.getWFlowFormStepsByCnd(mapParams);
	}

	@Override
	public void updateWFlowFormStepsByMap(Map<String, Object> mapParams) {
		extWFlowFormStepsMapper.updateWFlowFormStepsByMap(mapParams);
	}
    
    /***
     * 根据formi和任务id，获取WFlowFormSteps对象
     */
	@Override
	public WFlowFormSteps getWFlowFormStepsByFormAndTaskId(String flowFormId,String taskId) {
		WFlowFormSteps wFlowFormSteps = null;
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		List<String> lstTaskIds = Lists.newArrayList();
		lstTaskIds.add(taskId);
		param.put("flowFormId", flowFormId);
		param.put("lstTaskIds", lstTaskIds);
		List<WFlowFormSteps> lstWFlowFormSteps = extWFlowFormStepsMapper.getWFlowFormStepsByCnd(param);
		if(CollectionUtils.isNotEmpty(lstWFlowFormSteps)) {
			wFlowFormSteps = lstWFlowFormSteps.get(0);
		}
		return wFlowFormSteps;
	}

	@Override
	public Map<Integer, Integer> getNodeStatusByFormId(String formId) {
		List<WFlowFormSteps> list = extWFlowFormStepsMapper.getNodeStatusByFormId(formId);

		Map<Integer,Integer> map = Maps.newHashMapWithExpectedSize(list.size());
		for(WFlowFormSteps w:list){
			if(map.get(w.getLineStatus())==null)
				map.put(w.getLineStatus(),w.getDoneFlag());
		}
		return map;
	}


}
